package Reminder.view.components;


import com.jfoenix.controls.JFXButton;
import com.jfoenix.svg.SVGGlyph;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import Reminder.view.Controller;
import Reminder.view.GlobalConfig;
import Reminder.view.Main;
import Reminder.view.pages.pageReminder;

import java.awt.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class RemindCell extends BorderPane {
    int remindId;
    int index;
    Label titleLabel;
    Label contentLabel;
    SimpleDateFormat format;
    Controller controller;
    Stage stage;
    Timer timer;
    Label dateLabel;
    Date remindTime;
    public JFXButton deleteButton;
    boolean isActive;
    public RemindCell(int  index, int remindId, String title, String content, Date startTime, Controller controller, Stage stage){
        this.index = index;
        this.controller = controller;
        this.stage = stage;
        this.remindTime = startTime;
        this.remindId = remindId;
        titleLabel = new Label(title);
        contentLabel = new Label(content);
        format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        dateLabel = new Label(format.format(startTime));
        deleteButton = new JFXButton("");
        SVGGlyph delete = new SVGGlyph(0,
                "FULLSCREEN",
                "M815.3 362.1c-16.6 0-30 13.4-30 30V799c0 37.9-23.1 70-50.4 70h-87.6V392c0-16.4-13.5-29.9-30-29.9s-30 13.5-30 30V869H438.2V392.1c0-16.5-13.5-30-30-30s-30 13.5-30 30.1V869h-87.5c-27.3 0-50.4-32.1-50.4-70V392.1c0-16.6-13.4-30-30-30s-30 13.4-30 30V799c0 71.7 49.5 130 110.4 130h444.1c60.9 0 110.5-58.3 110.5-130V392.1c0-16.6-13.4-30-30-30zM897.8 227.1h-770c-16.5 0-30 13.5-30 30s13.5 30 30 30h770c16.5 0 30-13.5 30-30s-13.5-30-30-30zM356.8 166.1h312c16.5 0 30-13.6 30-30.2s-13.5-30.2-30-30.2h-312c-16.5 0-30 13.6-30 30.2s13.5 30.2 30 30.2z",
                Color.rgb(72, 170, 135));
        delete.setSize(20);


        delete.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        getTimer().cancel();
                        if(!GlobalConfig.IS_LOGIN){
                            GlobalConfig.ReminderList.remove(getIndex());
                            try {
                                getController().saveRemindList(GlobalConfig.ReminderList);
                            } catch (IOException | ParseException e) {
                                e.printStackTrace();
                            }
                            try {
                                pageReminder pageReminder = new pageReminder(getStage(),getController());
                            } catch (IOException | ParseException | AWTException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
        );

        deleteButton.setGraphic(delete);
        deleteButton.setRipplerFill(Color.WHITE);


        titleLabel.getStyleClass().add("RemindCell-title");
        contentLabel.getStyleClass().add("RemindCell-content");
        dateLabel.getStyleClass().add("RemindCell-date");



        this.setTop(titleLabel);
        this.setLeft(contentLabel);
        this.setRight(deleteButton);
        this.setBottom(dateLabel);
        this.setPadding(new Insets(15));
        System.out.println(getRemindTime());
        TimerTask remind = new TimerTask() {
            @Override
            public void run() {
                if(new Date().getTime() / 1000 == getRemindTime().getTime() / 1000){
                    try {
                        getController().displayTray(getTitleLabel().getText(),getContentLabel().getText(),"info");
                    } catch (AWTException e) {
                        e.printStackTrace();
                    }
                    this.cancel();
                }
                if (new Date().getTime() / 1000 > getRemindTime().getTime() / 1000 ){
                    this.cancel();
                }
            }
        };
        timer = new Timer();
        timer.schedule(remind,0,500);
        this.getStylesheets().add(Main.class.getResource("/css/remindCell.css").toExternalForm());
    }


    public Label getTitleLabel() {
        return titleLabel;
    }

    public void setTitleLabel(Label titleLabel) {
        this.titleLabel = titleLabel;
    }

    public Label getContentLabel() {
        return contentLabel;
    }

    public void setContentLabel(Label contentLabel) {
        this.contentLabel = contentLabel;
    }

    public SimpleDateFormat getFormat() {
        return format;
    }

    public void setFormat(SimpleDateFormat format) {
        this.format = format;
    }

    public Label getDateLabel() {
        return dateLabel;
    }

    public void setDateLabel(Label dateLabel) {
        this.dateLabel = dateLabel;
    }

    public Controller getController() {
        return controller;
    }

    public int getIndex() {
        return index;
    }

    public Stage getStage() {
        return stage;
    }
    public Timer getTimer() {
        return timer;
    }
    public Date getRemindTime() {
        return remindTime;
    }
    public int getRemindId() {
        return remindId;
    }

}
